package com.kkelectronics.w2b.beangen.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import com.kkelectronics.w2b.beangen.service.model.BeanField;

public class BeanParser {

	public String getClassComment(File f) {
		String comment = "";

		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(f));
			String str = "";
			if (br != null) {
				try {
					while ((str = br.readLine()) != null) {
						str = str.trim();
						if (str.contains("<summary>")) {
							while (!(str = br.readLine()).contains("</summary>")) {
								comment += str.replace("///", "").trim();
								comment += " ";
							}
							break;
						}
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					// e.printStackTrace();
				}
			}

			br.close();

		} catch (IOException e2) {
			System.out.println("FileNotFoundException - Caught " + f.getPath().toString());
		}
		return comment.trim();
	}

	public List<BeanField> parseFileForFields(File f) throws FileFormattingException {
		List<BeanField> res = new LinkedList<BeanField>();
		BeanField field = null;

		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(f));
			String str = "";
			String comment = "";
			String[] split;
			if (br != null) {
				try {
					while ((str = br.readLine()) != null) {
						str = str.trim();
						if ((!str.startsWith("//")) || str.startsWith("///")) {
							if (!str.contains("public class")) {
								if (str.contains("<summary>")) {
									while (!(str = br.readLine()).contains("</summary>")) {
										comment += str.replace("///", "").trim();
										comment += " ";
									}
									;
								}
								if (str.contains("public")) {
									split = str.replaceAll("( \\{ get; set; \\}|public )", "").split(" ");
									field = new BeanField(split[1]);
									Class<?> type = getType(split[0]);
									if (type != null) {
										field.setTypeShortName(type.getSimpleName());
										field.setTypeName(type.getName());
										field.setRawType(true);
									} else if (split[0].contains("List")) {
										field.setTypeShortName(split[0]);
										field.setTypeName(List.class.getName());
									} else {
										field.setTypeShortName(split[0] + "JS");
										field.setTypeName("com.kk_electronic.w2b.legacy.datajs.js." + split[0] + "JS");
									}
									field.setComment(comment.trim());
									comment = "";
									res.add(field);
								}
							}
						}
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					// e.printStackTrace();
				}
			}

			br.close();

		} catch (IOException e2) {
			System.out.println("FileNotFoundException - Caught " + f.getPath().toString());
		}
		return res;
	}

	private Class<?> getType(String string) {
		if ("string".equals(string)) {
			return String.class;
		} else if ("Guid".equals(string)) {
			return String.class;
		} else if ("bool".equals(string)) {
			return Boolean.class;
		} else if ("int".equals(string)) {
			return Integer.class;
		} else if ("double".equals(string)) {
			return Double.class;
		} else {
			return null;

		}
	}

	private String getListType(String string) {
		return string.replace("List", "JsArray").substring(0, string.length() - 1) + "JS>";
	}
}
